/*
 * @(#)HobakGonuAI.java $version 2013. 9. 9.
 *
 */

package com.pace.gonu.ai;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.pace.gonu.ai.model.BoardPathWithScore;
import com.pace.gonu.ai.model.PlayRockWithMovePath;
import com.pace.gonu.constants.Order;
import com.pace.gonu.model.PlayRock;
import com.pace.gonu.model.chnl.PlayChannel;
import com.pace.gonu.model.rule.HobakPlayRule;
import com.pace.gonu.model.status.HobakPlayStatus;
import com.pace.gonu.model.status.PlayStatus;
import com.pace.gonu.play.PlayService;

/**
 * @author pace
 */
public class HobakGonuAI implements GonuAI {
	private List<PlayRock> testPlayRocks = new ArrayList<PlayRock>();
	@Autowired
	private PlayService playService;

	private HobakPlayRule hobakPlayRule = new HobakPlayRule();

	@Override
	public void play(PlayChannel playChannel) {

		PlayStatus playStatus = playChannel.getPlayStatusObject();

		List<PlayRock> playRocks = playStatus.getPlayRocks();
		testPlayRocks.addAll(playRocks);
		
		//1. 지는 자리에 두지 않는다.
		List<PlayRockWithMovePath> aiPlayRockMvPaths = new ArrayList<PlayRockWithMovePath>();
		// 놀수 있는 모든 경우의 수를 구한다.
		for (PlayRock testPlayRock : testPlayRocks) {
			if (StringUtils.equals(testPlayRock.getOrder().toString(), Order.WHITE.toString())) {
				PlayRockWithMovePath playRockMvPath = new PlayRockWithMovePath();
				playRockMvPath.setPlayRock(testPlayRock);
				playRockMvPath.setBoardPathList(hobakPlayRule.getMovePath(playStatus.getPlayRocks(), testPlayRock));
				aiPlayRockMvPaths.add(playRockMvPath);
			}
		}

		for (PlayRockWithMovePath aiPlayRockMvPath : aiPlayRockMvPaths) {
			PlayRock aiPlayRock = aiPlayRockMvPath.getPlayRock();
			
			for (PlayRock testCPlayRock : testPlayRocks) {
				if (testCPlayRock.getInitPathIndex() == aiPlayRock.getInitPathIndex()) {
					for (BoardPathWithScore boardPathScoreC : aiPlayRockMvPath.getBoardPathWithScores()) {
						int tmpCX = testCPlayRock.getPath().getX();
						int tmpCY = testCPlayRock.getPath().getY();
						testCPlayRock.getPath().setX(boardPathScoreC.getBoardPath().getX());
						testCPlayRock.getPath().setY(boardPathScoreC.getBoardPath().getY());

						List<PlayRockWithMovePath> userPlayRockMvPaths = new ArrayList<PlayRockWithMovePath>();
						for (PlayRock userPlayRock : testPlayRocks) {
							if (StringUtils.equals(userPlayRock.getOrder().toString(), Order.BLACK.toString())) {
								PlayRockWithMovePath playRockMvPath = new PlayRockWithMovePath();
								playRockMvPath.setPlayRock(userPlayRock);
								playRockMvPath.setBoardPathList(hobakPlayRule.getMovePath(playStatus.getPlayRocks(), userPlayRock));
								userPlayRockMvPaths.add(playRockMvPath);
							}
						}

						for (PlayRockWithMovePath userPlayRockMvPath : userPlayRockMvPaths) {
							PlayRock userPlayRock = userPlayRockMvPath.getPlayRock();
							for (PlayRock testPPlayRock : testPlayRocks) {
								if (testPPlayRock.getInitPathIndex() == userPlayRock.getInitPathIndex()) {
									for (BoardPathWithScore boardPathScoreP : aiPlayRockMvPath.getBoardPathWithScores()) {
										int tmpPX = testPPlayRock.getPath().getX();
										int tmpPY = testPPlayRock.getPath().getY();
										
										testPPlayRock.getPath().setX(boardPathScoreP.getBoardPath().getX());
										testPPlayRock.getPath().setY(boardPathScoreP.getBoardPath().getY());

										HobakPlayStatus hobakTestPlayStatus = new HobakPlayStatus();
										hobakTestPlayStatus.getPlayRocks().addAll(testPlayRocks);
										if (hobakPlayRule.isEndGame(hobakTestPlayStatus, Order.BLACK)) {
											aiPlayRockMvPath.add();
											boardPathScoreC.add();
											
										} else if (hobakPlayRule.isEndGame(hobakTestPlayStatus, Order.WHITE)) {
											aiPlayRockMvPath.minus();
											boardPathScoreC.minus();
										} 
										
										testPPlayRock.getPath().setX(tmpPX);
										testPPlayRock.getPath().setY(tmpPY);
									}
								}
							}
						}
						testCPlayRock.getPath().setX(tmpCX);
						testCPlayRock.getPath().setY(tmpCY);
					}
				}
				
			}
		}


		PlayRockWithMovePath bestRock = new PlayRockWithMovePath();
		for (PlayRockWithMovePath aiPlayRockMvPath : aiPlayRockMvPaths) {
			if (CollectionUtils.isEmpty(aiPlayRockMvPath.getBoardPathWithScores())) {
				continue;
			}
			if (bestRock.getRockScore() <= aiPlayRockMvPath.getRockScore()) {
				bestRock = aiPlayRockMvPath;
			}
		}

		BoardPathWithScore bestBoardPathWithScore = new BoardPathWithScore();
		for (BoardPathWithScore boardPathWithScore : bestRock.getBoardPathWithScores()) {
			if (bestBoardPathWithScore.getPathScore() <= boardPathWithScore.getPathScore()) {
				bestBoardPathWithScore = boardPathWithScore;
			}
		}

		bestRock.setBestBoardPathWithScore(bestBoardPathWithScore);

		if (bestRock.getPlayRock() != null) {
			for (PlayRock playRock : playRocks) {
				if (bestRock.getPlayRock().getInitPathIndex() == playRock.getInitPathIndex() && playRock.getOrderObject().equals(Order.WHITE)) {
					playRock.getPath().setX(bestRock.getBestBoardPathWithScore().getBoardPath().getX());
					playRock.getPath().setY(bestRock.getBestBoardPathWithScore().getBoardPath().getY());
					break;
				}
			}
		}

	}

}
